package com.base.map;

import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.UiSettings;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.CameraPosition;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.MyLocationStyle;
import com.amap.api.services.core.PoiItem;
import com.base.commonlib.base.CBaseBarMvpActivity;
import com.base.commonlib.base.adapter.BaseAdapter;
import com.base.commonlib.base.adapter.BaseHolder;
import com.base.commonlib.config.MvpConstants;
import com.base.map.databinding.ActivityMappoiBinding;

import java.io.File;

/**
 * Created by arvin on 2019/1/6.
 */
@Route(path = "/map/mapoi")
public class MapPoiActivity extends CBaseBarMvpActivity<MapPoiPresenter> implements AMap.OnMapLoadedListener ,MapPoiView{

    private ActivityMappoiBinding mBinding;
    private Bundle mBundle;
    private AMap mMap;
    private MarkerOptions mMarkerOptions;
    private MapPoiPresenter mPresenter;
    private BaseAdapter<PoiItem> mAdapter;
    private Marker mMarker;

    private boolean isSetBottomSheetHeight;
    @Autowired
    double latitude;
    @Autowired
    double longitude;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        mBundle=savedInstanceState;
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_mappoi;
    }

    @Override
    protected void init() {
        super.init();
        mBinding = (ActivityMappoiBinding) mRootBinding;
        setBackNavigation();
        setBarTitle("地图选点");
        mBinding.textmap.onCreate(mBundle);

    }

    @Override
    protected void start() {
        mPresenter = new MapPoiPresenter(this,this);
        mPresenter.setALLude(latitude,longitude);
        mAdapter = new BaseAdapter<PoiItem>(mPresenter.mPoiItems, mContext) {

            @Override
            public void convert(BaseHolder holder, PoiItem poiItem, boolean isHeader, boolean isBottom) {

                TextView title=holder.getView(R.id.title);
                TextView address=holder.getView(R.id.address);
                title.setText(poiItem.getTitle());
                address.setText(poiItem.getSnippet());

            }
        };
        mAdapter.putNormalType(R.layout.item_poi);
        mAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseHolder holder, int dataPosition, boolean isHeader, boolean isBottom) {
                mPresenter.senAddress(mPresenter.mPoiItems.get(dataPosition));
                finish();
            }
        });
        mBinding.rv.setLayoutManager(new LinearLayoutManager(mContext));
        mBinding.rv.setAdapter(mAdapter);

        initMap();
    }



    private void initMap() {
        mMap = mBinding.textmap.getMap();
        File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + MvpConstants.MAPFILEPATH);
        if(file.exists())
        {
            mMap.setCustomMapStylePath(Environment.getExternalStorageDirectory().getAbsolutePath()+ MvpConstants.MAPFILEPATH);
            mMap.setMapCustomEnable(true);
        }
        UiSettings uiSettings = mMap.getUiSettings();
        uiSettings.setZoomControlsEnabled(false);
        mMap.moveCamera(CameraUpdateFactory.zoomTo(17));
        mMarkerOptions = new MarkerOptions();
        mMarkerOptions.icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
                .decodeResource(getResources(), R.drawable.purple_pin)));
        mMarkerOptions.anchor(0.5f,0.5f);
        MyLocationStyle myLocationStyle = new MyLocationStyle();
        myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_SHOW);
        myLocationStyle.showMyLocation(true);
        myLocationStyle.strokeColor(Color.parseColor("#1ca1fe"));
        myLocationStyle.radiusFillColor(Color.parseColor("#551ca1fe"));
        myLocationStyle.myLocationIcon(BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.ic_mylocation)));
        mMap.getUiSettings().setMyLocationButtonEnabled(true);
        mMap.setMyLocationStyle(myLocationStyle);
        mMap.setMyLocationEnabled(true);
        mMap.setOnMapLoadedListener(this);


    }
    @Override
    public void onMapLoaded() {
        mMap.setOnCameraChangeListener(new AMap.OnCameraChangeListener() {
            @Override
            public void onCameraChange(CameraPosition cameraPosition) {

            }

            @Override
            public void onCameraChangeFinish(CameraPosition cameraPosition) {
                mPresenter.geocodeSearch(cameraPosition.target.latitude,cameraPosition.target.longitude);
            }
        });
        mPresenter.start();
    }

    @Override
    public void addMarker(double latitude, double longitude) {
        LatLng latLng = new LatLng(latitude, longitude);
        if(mMarker==null)
        {
            mMarker = mMap.addMarker(mMarkerOptions);
            mMarker.setPositionByPixels(mBinding.textmap.getWidth()/2,mBinding.textmap.getHeight()/2);
        }
        mMap.moveCamera(CameraUpdateFactory.newCameraPosition(new CameraPosition(latLng, 17, 0, 0)));
    }

    @Override
    public void setInfo() {
        mAdapter.notifyDataSetChanged();
        if(mAdapter.getItemCount()>0)
        {
            mBinding.rv.scrollToPosition(0);
        }

    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (!isSetBottomSheetHeight) {
            CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) mBinding.sheet.getLayoutParams();
            layoutParams.height = mBinding.cl.getHeight();
            mBinding.sheet.setLayoutParams(layoutParams);
            isSetBottomSheetHeight = true;
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        mBinding.textmap.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mBinding.textmap.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(mMarker!=null)
        {
            mMarker.destroy();
        }
        mBinding.textmap.onDestroy();
        mPresenter.release();
    }



}
